package com.pti.usermanager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pti.usermanager.dao.api.RoleDao;
import com.pti.usermanager.dao.po.RolePo;
import com.pti.usermanager.enums.ConstantCode;
import com.pti.usermanager.service.api.RoleService;
import com.pti.usermanager.web.dto.request.*;
import com.pti.usermanager.web.dto.response.ResultResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: RoleServiceImpl
 * @Author: mahuilin
 * @Description: 角色业务实现层
 * @Date: 2019/1/17
 */
@Service
public class RoleServiceImpl implements RoleService {

    private static final Logger logger = LogManager.getLogger(RoleServiceImpl.class);

    @Autowired
    private RoleDao roleDao;



    /**
     * @Author mahuilin
     * @Description 分页模糊查询角色
     * @Date 2019/1/17
     */
    @Override
    public PageInfo<RolePo> findAll(String roleNm, Integer page, Integer pageSize) {

        logger.debug("roleNm:{} page:{} pageSize:{}",roleNm,page,pageSize);
        //pageHelper工具分页
        PageHelper.startPage(page,pageSize);
        List<RolePo> rolePoList = roleDao.findAll(roleNm);
        PageInfo<RolePo> pageInfo = new PageInfo<>(rolePoList);
        return pageInfo;
    }


    /**
     * @Author liumengyao
     * @Description 根据角色id查询对象
     * @Date 2019/1/27
     */
    @Override
    public ResultResponse findAllByRoleId(Long roleId) {
        logger.debug("roleId:{}",roleId);
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(roleDao.findAllByRoleId(roleId));
        return resultResponse;
    }

    /**
     * @Author mahuilin
     * @Description 添加角色
     * @Date 2019/1/17
     */
    @Override
    public ResultResponse saveRole(SaveRoleRequest saveRoleRequest) {

        logger.debug("saveRoleRequest:{}",saveRoleRequest.toString());

        ResultResponse resultResponse = new ResultResponse();
        try {
            roleDao.saveRole(saveRoleRequest);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    /**
     * @Author mahuilin
     * @Description 更新角色
     * @Date 2019/1/17
     */
    @Override
    public ResultResponse updateRole(UpdateRoleRequest updateRoleRequest) {

        logger.debug("updateRoleRequest:{}",updateRoleRequest.toString());

        ResultResponse resultResponse = new ResultResponse();

        try{
            roleDao.updateRole(updateRoleRequest);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    /**
     * @Author mahuilin
     * @Description 删除角色
     * @Date 2019/1/17
     */
    @Override
    public ResultResponse delRole(DeleteRoleRequest deleteRoleRequest) {

        logger.debug("deleteRoleRequest:{}",deleteRoleRequest.toString());

        ResultResponse resultResponse = new ResultResponse();

        try{
            for(Long roleId:deleteRoleRequest.getRoleId()){
                roleDao.delRole(roleId);
            }
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }


    /**
     * @Author liumengyao
     * @Description 角色分配用户
     * @Date 2019/1/19
     */
    @Override
    public ResultResponse saveUserByRidAndUid(SaveUserByRidAndUid saveUserByRidAndUid) {

        logger.debug("saveUserByRidAndUid:{}",saveUserByRidAndUid);

        ResultResponse resultResponse = new ResultResponse();
        try{
            //删除全部关联数据 添加所选中的
            roleDao.deleteUserByRid(saveUserByRidAndUid.getRoleId());
            for(Long usrId:saveUserByRidAndUid.getUsrId()){
                roleDao.saveUserByRidAndUid(saveUserByRidAndUid.getRoleId(),usrId);
            }
            return resultResponse;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }

    /**
     * @Author liumengyao
     * @Description 角色分配菜单
     * @Date 2019/1/19
     */
    @Override
    public ResultResponse saveMenuByRidAndUid(SaveMenuByRidAndUid saveMenuByRidAndUid) {

        logger.debug("saveMenuByRidAndUid:{}",saveMenuByRidAndUid.toString());

        ResultResponse resultResponse = new ResultResponse();
        try {
            //删除全部关联数据 添加所选中的
            roleDao.deleteMenuByRid(saveMenuByRidAndUid.getRoleId());
            for(Long menuId:saveMenuByRidAndUid.getMenuId()){
                roleDao.saveMenuByRidAndMid(saveMenuByRidAndUid.getRoleId(),menuId);
            }
            return resultResponse;
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }
}
